package com.evil.common.core.util;

import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
public class NumberUtils extends NumberUtil {

    public static Integer null2Zero(Integer i) {
        return null == i ? 0 : i;
    }

    public static Double toDouble(String str, Double defaultValue) {
        if (StringUtils.isNotBlank(str)) {
            try {
                return new Double(str);
            } catch (NumberFormatException e) {
                if (Objects.nonNull(defaultValue)) {
                    return defaultValue;
                } else {
                    throw e;
                }
            }
        }
        return defaultValue;
    }

    public static BigDecimal toBigDecimal(String str, BigDecimal defaultValue) {
        if (StringUtils.isNotBlank(str)) {
            try {
                return new BigDecimal(str);
            } catch (NumberFormatException e) {
                if (Objects.nonNull(defaultValue)) {
                    return defaultValue;
                } else {
                    throw e;
                }
            }
        }
        return defaultValue;
    }

    /**
     * 是否是数字类型
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isNum(Object obj) {
        return obj instanceof BigDecimal || obj instanceof Integer || obj instanceof Double || obj instanceof Long;
    }

    /**
     * 转成字符串
     *
     * @param obj          obj
     * @param scale        精度
     * @param roundingMode roundingMode
     * @return String
     */
    public static String toString(Object obj, Integer scale, RoundingMode roundingMode) {
        String result = Objects.nonNull(obj) ? obj.toString() : "";
        if (obj instanceof BigDecimal) {
            BigDecimal bigDecimal = (BigDecimal) obj;
            result = bigDecimal.toPlainString();
            if (Objects.nonNull(scale)) {
                result = bigDecimal.setScale(scale, roundingMode).toPlainString();
            }
        }
        if (obj instanceof Integer) {
            int integer = (Integer) obj;
            result = Integer.toString(integer);
            if (Objects.nonNull(scale)) {
                result = new BigDecimal(integer).setScale(scale, roundingMode).toPlainString();
            }
        }
        if (obj instanceof Double) {
            double aDouble = (Double) obj;
            result = Double.toString(aDouble);
            if (Objects.nonNull(scale)) {
                result = new BigDecimal(aDouble).setScale(scale, roundingMode).toPlainString();
            }
        }
        if (obj instanceof Long) {
            long aLong = (long) obj;
            result = Long.toString(aLong);
            if (Objects.nonNull(scale)) {
                result = new BigDecimal(aLong).setScale(scale, roundingMode).toPlainString();
            }
        }
        if (obj instanceof String) {
            BigDecimal decimal = NumberUtils.toBigDecimal(result, BigDecimal.ZERO).stripTrailingZeros();
            if (Objects.nonNull(scale)) {
                result = decimal.setScale(scale, roundingMode).toPlainString();
            }
        }
        return result;
    }
}
